package cn.itcast.solr;

import cn.itcast.solr.pojo.Heima60;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class IndexQueryTest {



    @Test
    public void testQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = (Long) result.get("price");
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }

    @Test
    public void testQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }


    /**
     * 范围查询，两个数值表示边界，关系为左右包含  >=,<=
     *
     * @throws SolrServerException
     */
    @Test
    public void testRangeQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("price:[* TO 199999]");

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }


    /**
     * AND   &&
     *
     * OR  ||
     *
     * NOT  !
     *
     * @throws SolrServerException
     */
    @Test
    public void testBooleanQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:手机 AND price:[300000 TO 350000] NOT title:中兴");

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }


    @Test
    public void testSortQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //给查询条件对象设置排序条件，并添加排序方式
        solrQuery.setSort("price", SolrQuery.ORDER.desc);

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }

    @Test
    public void testPageQueryIndexWithBean() throws SolrServerException {

        int currentPage = 5;
        final int PAGE_SIZE = 4;
        int start = (currentPage-1)*PAGE_SIZE;


        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //给查询条件对象设置排序条件，并添加排序方式

        solrQuery.setStart(start);
        solrQuery.setRows(PAGE_SIZE);

        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+title+" price:"+price);
        });

    }

    @Test
    public void testHighQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //定义查询条件
        SolrQuery solrQuery = new SolrQuery("title:小米");
        //添加高亮条件
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");
        solrQuery.addHighlightField("title");



        //执行查询并获取响应结果
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //双层map的第一层内容就是当前高亮文档的id，第二层map的key内容是高亮字段的名称，值是高亮的结果
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();


        //解析查询的结果
        List<Heima60> beans = queryResponse.getBeans(Heima60.class);

        beans.forEach(bean->{

            String id = bean.getId();

            //根据id获取到第二层map，根据高亮字段获取第二层map的值
            String highLightTitle = highlighting.get(id).get("title").get(0);
            //String title = bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id = " + id+" title:"+highLightTitle+" price:"+price);
        });

    }
}
